package edu.hawaii.duedates.libraries;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.HttpUnitOptions;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;

/**
 * Hawaii State Library information will be generated from this class.
 * 
 * @author Ka Hung Phillip Lau
 * @author Creighton Okada
 */
public class HawaiiStateLibrary {
  /**
   * Attempts to login to the Hawaii State Library System.
   * 
   * @param id The hawaii library id barcode.
   * @param password The password (last four digits of your phone number).
   * @return The WebConversation after logging in.
   * @throws Exception If errors occur while logging in.
   */
  private WebConversation loginToHawaiiStateLibrary(String id, String password) throws Exception {
    try {
      String url = "http://www.librarieshawaii.org/";
      // Website has bogus JavaScript code.
      HttpUnitOptions.setExceptionsThrownOnScriptError(false);
      // create the conversation object which will maintain state for us
      WebConversation conversation = new WebConversation();
      WebRequest request = new GetMethodWebRequest(url);
      WebResponse response = conversation.getResponse(request);
      WebLink link = response.getLinkWith("My Account");
      link.click();
      response = conversation.getCurrentPage();
      WebForm form = response.getForms()[0];
      form.setParameter("sec1", id);
      form.setParameter("sec2", password);
      form.submit();
      return conversation;
    }
    catch (Exception e) {
      throw new Exception("Error while logging in to Hawaii State Library", e);
    }
  }
  
  /**
   * Gets info from the Hawaii State Library site.
   * 
   * @param idLogin The barcode number from the library card.
   * @param password The password (normally the last four digits of your phone number).
   * @return A list of LibraryBooks providing information about checked out books.
   * @throws Exception If problems occur getting the information.
   */
  public List<LibraryBook> getHawaiiStateInfo(String idLogin, String password) throws Exception {
    try {
      WebConversation conversation = this.loginToHawaiiStateLibrary(idLogin, password);
      WebResponse response = conversation.getCurrentPage();
      WebLink link = response.getLinkWith("Items Out");
      link.click();
      response = conversation.getCurrentPage();
      List<LibraryBook> books = new ArrayList<LibraryBook>();
      boolean noBooksOut = response.getText().contains("Checked Out: 0");
      if (!noBooksOut) {
        WebTable table = response.getTableStartingWith("Title");
        int rowCount = table.getRowCount();
        DateFormat newFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
        for (int i = 1; i < rowCount; i++) {
          Date date = newFormat.parse(table.getCellAsText(i, 2));
          LibraryBook tempBook = new LibraryBook(table.getCellAsText(i, 0).replace("\n", ""),date);
          books.add(tempBook);
        }
      }
      return books;
    }
    catch (Exception e) {
      throw new Exception("Error getting Hawaii State Library info", e);
    }
  }
}
